package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.PayType;

import java.util.List;

/**
 * 预购支付类型适配器
 *
 * @author wenqin 2017-07-13 10:22
 */

public class PayTypeAdapter extends RecyclerView.Adapter<PayTypeAdapter.Vh> {

    private Context mContext;
    private List<PayType> mPayTypes;
    private LayoutInflater mInflater;
    private int mSelectedPosition = 0;
    private OnCheckClick mOnCheckClick;

    public PayTypeAdapter(Context context, List<PayType> payTypes) {
        mContext = context;
        mPayTypes = payTypes;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.adapter_pay_type, parent, false));
    }

    @Override
    public void onBindViewHolder(Vh holder, int position) {
        PayType payType = mPayTypes.get(position);
        switch (payType.getPayType()) {
            case PayType.TYPE_ALI_PAY:
            case PayType.TYPE_ALI_PAY_SCAN:
                holder.mTypeIcon.setImageResource(R.mipmap.alipay);
                break;
            case PayType.TYPE_WE_CHAT_PAY_SCAN:
                holder.mTypeIcon.setImageResource(R.mipmap.we_chat);
                break;
            case PayType.TYPE_QQ_PAY:
                holder.mTypeIcon.setImageResource(R.mipmap.qq_icon);
                break;
            case PayType.TYPE_QUICK_PAY:
                holder.mTypeIcon.setImageResource(R.mipmap.unlon_pay);
                break;
        }

        holder.mTypeName.setText(payType.getCation());
        if (mSelectedPosition == position) {
            holder.mCheckBox.setChecked(true);
        } else {
            holder.mCheckBox.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return mPayTypes == null ? 0 : mPayTypes.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        ImageView mTypeIcon;
        TextView mTypeName;
        CheckBox mCheckBox;

        public Vh(View itemView) {
            super(itemView);
            mTypeIcon = itemView.findViewById(R.id.type_icon);
            mTypeName = itemView.findViewById(R.id.type_name);
            mCheckBox = itemView.findViewById(R.id.check_box);
            mCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mSelectedPosition >= 0) {
                        notifyItemChanged(mSelectedPosition);
                    }
                    mSelectedPosition = getAdapterPosition();
                    if (mOnCheckClick != null) {
                        mOnCheckClick.checkBoxClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnCheckClick {
        void checkBoxClick(int position);
    }

    public PayType getSelectedPayType() {
        return mPayTypes.get(mSelectedPosition);
    }

    public void setOnCheckClick(OnCheckClick onCheckClick) {
        mOnCheckClick = onCheckClick;
    }
}
